package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.bean.CmsUser;
import com.briup.cms.bean.extend.CmsUserExtend;
import com.briup.cms.dao.CmsUserDao;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.ICmsUserService;
import com.briup.cms.util.MD5Utils;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @BelongsProject: CMS
 * @BelongsPackage: com.briup.cms.service.impl
 * @Author: 韩振阳
 * @CreateTime: 2024-11-21  11:05
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class CmsUserServiceImpl implements ICmsUserService {
    @Autowired
    private CmsUserDao cmsUserDao;

    @Override
    public CmsUser login(String username, String password) {
        String md5 = MD5Utils.MD5(password);
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", username);
        List<CmsUser> users = cmsUserDao.selectByMap(map);
        CmsUser user = users.get(0);
        if (!user.getPassword().equals(md5))
            throw new ServiceException(ResultCode.DATA_WRONG);
        return user;
    }

    @Override
    public void save(CmsUser cmsUser) {
        String password = cmsUser.getPassword();
        String username = cmsUser.getUsername();
        if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            throw new ServiceException(ResultCode.DATA_WRONG);
        }
        cmsUser.setPassword(MD5Utils.MD5(password));
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", username);
        List<CmsUser> CmsCmsUsers = cmsUserDao.selectByMap(map);
        if (!CmsCmsUsers.isEmpty()) {
            throw new ServiceException(ResultCode.DATA_EXISTED);
        }
        cmsUser.setRegisterTime(LocalDateTime.now());
        cmsUserDao.insert(cmsUser);
    }

    @Override
    public CmsUser queryById(Long id) {
        //1 .有效参数判断
        if (id == null)
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        //2 .查找用户
        CmsUser user = cmsUserDao.selectById(id);
        if (user == null)
            throw new ServiceException(ResultCode.DATA_NONE);
        return user;
    }

    @Override
    public void update(CmsUser user) {
        System.out.println("CmsUserServiceImpl.update");
        Long id = user.getId();
        CmsUser oldUser = cmsUserDao.selectById(id);
        if (Objects.isNull(id))
            throw new ServiceException(ResultCode.DATA_WRONG);
        LambdaUpdateWrapper<CmsUser> wrapper = new LambdaUpdateWrapper<>();
        String username = user.getUsername();
        if (!username.equals(oldUser.getUsername())) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("username", username);
            List<CmsUser> users = cmsUserDao.selectByMap(map);
            if (!users.isEmpty())
                throw new ServiceException(ResultCode.DATA_EXISTED);
        }
        wrapper.eq(CmsUser::getId, id);
        cmsUserDao.update(user, wrapper);
    }

    @Override
    public void setVip(Long id) {
        CmsUser user = cmsUserDao.selectById(id);
        if (user == null)
            throw new ServiceException(ResultCode.DATA_NONE);
        if (user.getIsVip() == 1) {
            LocalDateTime expiresTime = user.getExpiresTime();
            user.setExpiresTime(expiresTime.plusMonths(1));
        } else {
            user.setExpiresTime(LocalDateTime.now().plusMonths(1));
            user.setIsVip(1);
        }
    }

    @Override
    public void deleteByBatch(List<Long> ids) {
        //1.有效参数判断
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        //2.删除指定用户
        cmsUserDao.deleteBatchIds(ids);
    }

    @Override
    public IPage<CmsUserExtend> query(Integer pageNum, Integer pageSize, String username, String status, Integer roleId, Integer isVip) {
        if (pageNum == null || pageSize == null)
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);

        //关键点：程序员自行书写sql语句，要完成多表连接查询，如何进行分页处理？ 推荐下面写法
        IPage<CmsUserExtend> page = new Page<>(pageNum, pageSize);
        cmsUserDao.queryAllUserWithRole(page, username, status, roleId, isVip);
        //System .out .println (page .getTotal()) ;
        //System .out .println (page .getRecords () .size()) ;
        return page;
    }


}
